{
 "cells": [
  {
   "cell_type": "raw",
   "metadata": {
    "raw_mimetype": "text/restructuredtext"
   },
   "source": [
    ".. _nb_nsga3:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    ".. meta::\n",
    "   :description: NSGA-III (also known as NSGA3) has been proposed for many-objective optimization to address the shortcomings of its predecessor NSGA-II. Using reference directions, the algorithm scales to many objectives and achieves a diverse set of non-dominated solutions."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    ".. meta::\n",
    "   :keywords: NSGA3, NSGA-III, Many-objective Optimization, Reference Directions, Non-Dominated Sorting, Multi-objective Optimization, Python"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# NSGA-III\n",
    "\n",
    "\n",
    "The algorithm is implemented in base of <cite data-cite=\"nsga3-part1\"></cite> <cite data-cite=\"nsga3-part2\"></cite>. Implementation details of this algorithm can be found in <cite data-cite=\"nsga3-norm\"></cite>. NSGA-III is based on [Reference Directions](../../misc/reference_directions.ipynb) which need to be provided when the algorithm is initialized. \n",
    "\n",
    "The survival, first, the non-dominated sorting is done as in NSGA-II. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div style=\"text-align: center;\">\n",
    "    <img src=\"https://github.com/anyoptimization/pymoo-data/blob/main/docs/images/nsga3_survival_1.png?raw=true\" width=\"350\">\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Second, from the splitting front, some solutions need to be selected. NSGA-III fills up the underrepresented reference direction first. If the reference direction does not have any solution assigned, then the solution with the smallest perpendicular distance in the normalized objective space is surviving. In case a second solution for this reference line is added, it is assigned randomly. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div style=\"text-align: center;\">\n",
    "    <img src=\"https://github.com/anyoptimization/pymoo-data/blob/main/docs/images/nsga3_survival_2.png?raw=true\" width=\"350\">\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Thus, when this algorithm converges, each reference line seeks to find a good representative non-dominated solution."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from pymoo.algorithms.moo.nsga3 import NSGA3\n",
    "\n",
    "\n",
    "from pymoo.optimize import minimize\n",
    "from pymoo.problems import get_problem\n",
    "from pymoo.util.ref_dirs import get_reference_directions\n",
    "from pymoo.visualization.scatter import Scatter\n",
    "\n",
    "# create the reference directions to be used for the optimization\n",
    "ref_dirs = get_reference_directions(\"das-dennis\", 3, n_partitions=12)\n",
    "\n",
    "# create the algorithm object\n",
    "algorithm = NSGA3(pop_size=92,\n",
    "                  ref_dirs=ref_dirs)\n",
    "\n",
    "# execute the optimization\n",
    "res = minimize(get_problem(\"dtlz1\"),\n",
    "               algorithm,\n",
    "               seed=1,\n",
    "               termination=('n_gen', 600))\n",
    "\n",
    "Scatter().add(res.F).show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "res = minimize(get_problem(\"dtlz1^-1\"),\n",
    "               algorithm,\n",
    "               seed=1,\n",
    "               termination=('n_gen', 600))\n",
    "\n",
    "Scatter().add(res.F).show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "raw_mimetype": "text/restructuredtext"
   },
   "source": [
    "### API"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {
    "raw_mimetype": "text/restructuredtext"
   },
   "source": [
    ".. autoclass:: pymoo.algorithms.moo.nsga3.NSGA3\n",
    "    :noindex:"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
